import Vue from 'vue';
import VueRouter from 'vue-router';

Vue.use(VueRouter)

const router = new VueRouter({
    routes: [
        {
            path: '/index',
            component: ()=>import( '../pages/index.vue'),
            children: [//二级路由
                {
                    path: '/cart',
                    component: () => import('../views/cart.vue'),
                },
                {
                    path: '/home',
                    component:() => import( '../views/home.vue')
                },
                {
                    path: '/sort',
                    component:() => import( '../views/sort.vue')
                },
                {
                    path: '/usercenter',
                    component:() => import( '../views/usercenter.vue')
                },
                {//输入错误 二级路由 重定向到首页的home组件
                    path: '',
                    redirect:'/home'
                }
            ]
        },
        {
            path: '/detail/:id',
            component:()=>import( '../pages/detail.vue')
        },
        {
            path: '/list',
            component:()=>import( '../pages/list.vue')
        },
        {
            path: '/login',
            component:()=>import( '../pages/login.vue')
        },
        {
            path: '/order',
            component:()=>import( '../pages/order.vue')
        },
        {
            path: '/search',
            component:()=>import( '../pages/search.vue')
        },
        {
            path: '/register',
            component:()=>import( '../pages/register.vue')
        },
        // {
        //     path: '/searchNot',
        //     component:()=>import( '../components/searchNot.vue')
        // },
        {//一级路由重定向到首页组件
            path: '*',
            redirect:'/index'
        }
    ],
    mode:'history'
})

export default router

// router.beforeEach((to, from, next) => {
//     if (to.path == '/login') {//如果不写去的路径为login那么就会死循环
//         next()
//         return;
//     }
//     if (sessionStorage.getItem('login')) {
//         next()
//         return
//     }
//     next('/login');
// })